<?php
require 'include.php';
if($user = check_cookie()){
	$membre = new membre($user);
	echo "<h2>Calendrier</h2>\n";
	echo "<p class = 'center fade small'>cliquez sur un match pour afficher le d&eacute;tail</p>\n";
	$table = new table("matchs");
	$table->order = 'date';
	// $table->sortable = 1;
	$fields = array("date" => "date",
					"match" => "match",
					"info" => "info");
	foreach($fields as $field => $print){
		$$field = new table_field($field,'matchs');
		$$field->print = $field;
		if($field == 'info') $$field->styleclass = 'center';
		$table->fields[] = $$field;
	}
	$matchs = array();
	foreach($membre->equipes as $id_equipe => $nom_equipe){
		$equipe = new equipe($id_equipe);
		$matchs = array_merge($matchs,$equipe->matchs());
	}
	foreach($matchs as $match){
		$row = new table_row();
		foreach($fields as $field => $print){
			if($field == 'match'){
				if(stripos($match->local,"LUTRY") !== FALSE) $row->cols['match'] = "VBCLL - <span class = 'bold'>$match->visiteur</span>";
				else $row->cols['match'] = "<span class = 'bold'>$match->local</span> - VBCLL";
			}
			elseif($field == 'date') $row->cols['date'] = $match->date;
			elseif($field == 'info'){
				if($match->resultat) $row->cols['info'] = $match->format_resultat();
				elseif($match->date <= date("Y-m-d")){
					if($user_id = check_cookie()){
						$user = new membre($user_id);
						if((in_array($match->equipe,$user->equipes))||(check_admin())){
							$content = "<select onchange = 'score_match(\"$match->no\",this.options[this.selectedIndex].value)'>";
							$results = array("3-0","3-1","3-2","2-3","1-3","0-3");
							$content .= "<option value = ''>score</option>\n";
							foreach($results as $result) $content .= "<option value = '$result'>$result</option>";
							$content .= "</select>";															
						}
						else $content = "<span class = 'fade'>??</span>";
					}
					else $content = "<span class = 'fade'>??</span>";
					$row->cols['info'] = $content;
				} 
				else $row->cols['info'] = $match->heure;
			}
		}
		foreach($row->cols as $col_id => $col_val){
			if(strpos($col_val,"<select") === FALSE) $row->cols[$col_id] = "<span name = '".(str_replace("-","",$match->date))."' onclick = 'match_detail(\"$match->no\",$equipe->id,\"ma_page\")'>$col_val</span>";
		}
		$table->rows[] = $row;
	}
	foreach($membre->marquages as $marquage){
		$match = new match($marquage);
		$row = new table_row();
		$row->onclick = "onclick = 'match_detail(\"$match->no\",\"\",\"ma_page\")'";
		foreach($fields as $field){
			if($field == 'info' && $match->date > date('Y-m-d')) $echo = $match->heure;
			elseif($field == 'info') $echo = 'marq.';
			elseif($field == 'match'){
				$salle = new salle($match->salle);
				$echo = "Marq : ".$salle->lien();
			}
			else $echo = $match->$field;
			$row->cols[$field] = $echo;
		}
		// if($match->date < date('Y-m-d')) $row->hidden = 1;
		$table->rows[] = $row; 
	}
	foreach($membre->arbitrages as $arbitrage){
		$match = new match($arbitrage);
		$row = new table_row();
		$row->onclick = "onclick = 'match_detail(\"$match->no\",\"\",\"ma_page\")'";
		foreach($fields as $field){
			if($field == 'info' && $match->date > date('Y-m-d')) $echo = $match->heure;
			elseif($field == 'info') $echo = 'arb.';
			elseif($field == 'match'){
				$salle = new salle($match->salle);
				$echo = "Arb. : ".$salle->lien();
			}
			else $echo = $match->$field;
			$row->cols[$field] = $echo;
		}
		// if($match->date < date('Y-m-d')) $row->hidden = 1;
		$table->rows[] = $row; 
	}
	foreach($membre->entraineurs as $equipe_id => $equipe_nom){
		$equipe = new equipe($equipe_id);
		$matchs_coach = $equipe->matchs();
		foreach($matchs_coach as $match){
			$row = new table_row();
			foreach($fields as $field){
				if($field == 'match'){
					if(stripos($match->local,"LUTRY") !== FALSE) $row->cols['match'] = "VBCLL - <span class = 'bold'>$match->visiteur</span>";
					else $row->cols['match'] = "<span class = 'bold'>$match->local</span> - VBCLL";
				}
				elseif($field == 'date') $row->cols['date'] = $match->date;
				elseif($field == 'info'){
					if($match->resultat) $row->cols['info'] = $match->format_resultat();
					elseif($match->date <= date("Y-m-d")&&(str_replace(":","",$match->heure) > date("His"))){
						if($user_id = check_cookie()){
							$user = new membre($user_id);
							if((in_array($match->equipe,$user->equipes))||(check_admin())){
								$content = "<select onchange = 'score_match(\"$match->no\",this.options[this.selectedIndex].value)'>";
								$results = array("3-0","3-1","3-2","2-3","1-3","0-3");
								foreach($results as $result) $content .= "<option value = '$result'>$result</option>";
								$content .= "</select>";															
							}
							else $content = "<span class = 'fade'>??</span>";
						}
						else $content = "<span class = 'fade'>??</span>";
						$row->cols['info'] = $content;
					} 
					else $row->cols['info'] = $match->heure;
				}
			}
			foreach($row->cols as $col_id => $col_val){
				if(strpos($col_val,"<select") === FALSE) $row->cols[$col_id] = "<span onclick = 'match_detail(\"$match->no\",\"\",\"ma_page\")'>$col_val</span>";
			}
			$table->rows[] = $row;
		}
	}
	echo $table->print_header();
	echo $table->print_body();
	echo $table->print_footer();		
	
	
	if(count($membre->equipes)){
		echo "<h2>Communications</h2>\n";
		$wheres = array();
		foreach($membre->equipes as $equipe_id => $equipe_nom){
			$wheres[] .= "equipe_id = $equipe_id";
		}
		$where = " (".(implode(" or ",$wheres)).") ";
		echo "<div class = 'box' id = 'communications'>\n";
		$q = "select corps, date, concat(prenom,' ',membres.nom) as auteur, equipe_id,membre_id, communications.id as id, equipes.nom as equipe from communications inner join membres on (communications.membre_id = membres.id) inner join equipes on equipes.id = equipe_id where $where order by date desc";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$del = (($s->membre_id == $user)||(check_admin())) ? "<span class = 'float_right'>&nbsp;<img src = 'images/icon_delete.png' alt = 'supprimer' width = '12' height = '12' onclick = 'if(confirm(\"Confirmer la suppression de la communication ?\")) delete_communication($s->id,$s->equipe_id)' style = 'cursor:pointer;' /></span>" : "";
			echo "<p class = 'fade separation'>$s->equipe : <a href = 'javascript: membre_detail($s->membre_id)'>$s->auteur</a>, ".format_date($s->date).$del."</p>\n";
			echo "<p>".(stripslashes($s->corps))."</p>";
			echo "<hr style = 'margin-left: -10px; margin-right: -10px'/>\n";
		}
		if(!mysql_num_rows($r)) echo "<p class = 'center fade'>aucune communication</p>\n";
		echo "</div>\n";				
	}
	
}
else echo "Vous n'&ecirc;tes pas identifi&eacute;"
?>